<?php

require_once("util/connection.php");

require_once("model/article_video_ping/to/article_video_ping_to.php");
require_once("model/article_video_ping/dao/article_video_ping_dao.php");


class Mysql_article_video_ping_dao implements Article_video_ping_dao{

	public function create($article_video_ping_to) {
		try {
		
			$link = Connection::singleton()->transaction_begin();
			
			$to_article_video = $article_video_ping_to->get_to_article_video();
			$from_autor_login = mysql_real_escape_string($article_video_ping_to->get_from_autor_login());
			$comment = mysql_real_escape_string($article_video_ping_to->get_comment());
			$type = mysql_real_escape_string($article_video_ping_to->get_type());
			$date = mysql_real_escape_string($article_video_ping_to->get_date());	
			$state = mysql_real_escape_string($article_video_ping_to->get_state());				
			
			$result = mysql_query("insert into article_video_ping 
									(to_article_video, from_autor_login, comment, type, date, state)
									values ('$to_article_video', '$from_autor_login', '$comment', 
									'$type', '$date', '$state')", $link);			
			
			if (!$result || (mysql_affected_rows($link) != 1))
				throw new Exception("not row inserted");
			
			Connection::singleton()->transaction_commit($link);
			
			return $article_video_ping_to;
		}
		catch (Exception $e) {
			Connection::singleton()->transaction_rollback($link);
			throw new Exception("Internal error exception --> " . $e);
		}
	}
	
	
	public function exist($to_article_video, $from_autor_login) {

		try {
			$link = Connection::singleton()->transaction_begin();
			
			$from_autor_login = mysql_real_escape_string($from_autor_login);
			
			$result = mysql_query("select * from article_video_ping 
									where to_article_number = '$to_article_video'
									and from_autor_login like '$from_autor_login'", $link);
									
			if (!$result || (mysql_num_rows($result) != 1)) {
				Connection::singleton()->transaction_commit($link);
				return false;
			}
			
			Connection::singleton()->transaction_commit($link);			
			return true;
		}
		catch (Exception $e) {
			Connection::singleton()->transaction_rollback($link);
			throw new Exception("Internal error exception --> " . $e);
		}
	}
	
	
	public function find_by_article_video_and_autor_login($to_article_video, $from_autor_login) {
	
		try {
		
			$link = Connection::singleton()->transaction_begin();
			
			$from_autor_login = mysql_real_escape_string($from_autor_login);
			
			$result = mysql_query("select * from article_video_ping 
									where to_article_video = '$to_article_video'
									and from_autor_login like '$from_autor_login'", $link);
									
			if (!$result || (mysql_num_rows($result) != 1)) 
				throw new Exception("not row found");

			$aux = mysql_fetch_assoc($result);
			Connection::singleton()->transaction_commit($link);
			return new Article_video_ping_to($aux['to_article_video'], $aux['from_autor_login'], 
											$aux['comment'], $aux['type'], $aux['date'], $aux['state']);
			
		}
		catch (Exception $e) {			
			Connection::singleton()->transaction_rollback($link);
			throw new Exception("Internal error exception --> " . $e);		
		}
	}
	
	
	public function update($article_video_ping_to) {

		try {
				
			$link = Connection::singleton()->transaction_begin();
			
			$to_article_video = $article_video_ping_to->get_to_article_video();
			$from_autor_login = mysql_real_escape_string($article_video_ping_to->get_from_autor_login());
			$state = mysql_real_escape_string($article_video_ping_to->get_state());
			
			$result = mysql_query("update article_video_ping 
									set state='$state' 
									where to_article_video = '$to_article_video'
									and from_autor_login like '$from_autor_login'", $link);
							
			if (!$result || (mysql_affected_rows($link) != 1))
				throw new Exception("not row updated");
			
			Connection::singleton()->transaction_commit($link);
			
		}
		catch (Exception $e) {
			Connection::singleton()->transaction_rollback($link);
			throw new Exception("Internal error exception --> " . $e);
		}	
		
	}
	
	
	public function remove($to_article_video, $from_autor_login) {	
		try {			
			$link = Connection::singleton()->transaction_begin();
			
			$from_autor_login = mysql_real_escape_string($from_autor_login);
			
			$result = mysql_query("delete from article_video_ping
									where to_article_video = '$to_article_video'
									and from_autor_login like '$from_autor_login'", $link);
									
			if (!$result || (mysql_affected_rows($link) != 1))
				throw new Exception("not row deleted");
			
			Connection::singleton()->transaction_commit($link);
					
		}
		catch (Exception $e) {
			Connection::singleton()->transaction_rollback($link);
			throw new Exception("Internal error exception --> " . $e);
		}
	}
}
?>